package com.cqgc.service.impl;

import
        com.cqgc.exception.AddNotAllowedException;
import com.cqgc.exception.DeletionNotAllowedException;
import com.cqgc.exception.UpdateNotAllowedException;
import com.cqgc.mapper.CategoryMapper;
import com.cqgc.mapper.GoodsMapper;
import com.cqgc.pojo.dto.CategoryDTO.CategoryDTO;
import com.cqgc.pojo.dto.CategoryDTO.CategoryPageQueryDTO;
import com.cqgc.pojo.dto.CategoryDTO.CategoryUpdateDTO;
import com.cqgc.pojo.entity.Category;
import com.cqgc.pojo.entity.PageResult;
import com.cqgc.service.CategoryService;
import com.cqgc.utils.ThreadLocalUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.nio.file.attribute.UserPrincipalNotFoundException;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

@Service
public class CategoryServiceImpl implements CategoryService {

    @Autowired
    private CategoryMapper categoryMapper;

    @Autowired
    private GoodsMapper goodsMapper;

    /**
     * 分页查询
     * @param categoryPageQueryDTO
     * @return
     */
    @Override
    public PageResult pageQuery(CategoryPageQueryDTO categoryPageQueryDTO) {
        //开启分页
        PageHelper.startPage(categoryPageQueryDTO.getPage(),categoryPageQueryDTO.getPageSize());
        Page<Category> page = categoryMapper.pageQuery(categoryPageQueryDTO);
        return new PageResult(page.getTotal(),page.getResult());
    }
    @Override
    public void add(CategoryDTO categoryDTO) {
        if (categoryMapper.findByCategoryName(categoryDTO.getCategoryName())!=null){
            throw new AddNotAllowedException("该分类已存在");
        }
        Category category = new Category();
        BeanUtils.copyProperties(categoryDTO,category);
        //补充属性值
        category.setCreateTime(LocalDateTime.now());
        category.setUpdateTime(LocalDateTime.now());
        //获取当前用户id
        Map<String, Object> map = ThreadLocalUtil.get();
        Integer Userid = (Integer) map.get("uid");
        category.setCreateUser(Userid);
        category.setUpdateUser(Userid);
        categoryMapper.add(category);
    }

    @Override
    public Integer delete(Integer cid) {
        //判断该分类下是否有商品
        if (goodsMapper.findByCategoryId(cid).size()!=0){
            throw new DeletionNotAllowedException("该分类下有商品，不能删除");
        }
        return categoryMapper.delete(cid);
    }

    @Override
    public void update(CategoryUpdateDTO categoryUpdateDTO) {
        //查询新增分类的名称是否存在
        Category category = categoryMapper.findByCategoryName(categoryUpdateDTO.getCategoryName());
        if (category != null){
           throw new UpdateNotAllowedException("该分类名称已存在");

        }
        Category category1 = new Category();
        BeanUtils.copyProperties(categoryUpdateDTO,category1);
        Map<String, Object> map = ThreadLocalUtil.get();
        Integer Userid = (Integer) map.get("uid");

        category1.setUpdateTime(LocalDateTime.now());
        category1.setUpdateUser(Userid);

        categoryMapper.update(category1);

    }

    @Override
    public List<Category> findAll() {
        return categoryMapper.findAll();
    }


}
